﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Web;
using System.Net;
using System.Threading;
using System.Diagnostics;
using System.Text.RegularExpressions;
namespace H1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        
        private void submit_Click(object sender, EventArgs e)
        {
            string re1 = null;
            string re2 = null;
            this.Cursor = Cursors.WaitCursor;
            show.Text = "开始爬取";
            string worf = "http://www.baidu.com/s?wd=" + textBox1.Text;
            Thread thread1 = new Thread(() => {
                re1 = downText(worf);
            });
            Thread thread2 = new Thread(() => {
                re2 = downText(worf);
            });
            thread1.Start();
            thread2.Start();
            thread1.Join();
            thread2.Join();
            if (re1.Length > 200)
            {
                re1=re1.Substring(0,200);

            }
            if (re2.Length > 200)
            {
                re2=re2.Substring(0,200);

            }
            richTextBox2.Text = re2;
            richTextBox1.Text = re1;
            show.Text = "爬取结束";
            this.Cursor = Cursors.Default;

        }
        private string downText(string url)
        {
            Debug.WriteLine(url);

            WebClient webClient = new WebClient();
            webClient.Encoding = Encoding.UTF8;
            string html = webClient.DownloadString(url);
            string str = "";
            Debug.Write("html");
            Debug.WriteLine(html);
            foreach (var item in Regex.Matches(html, "[\u4e00-\u9fa5]+"))
            {
                str += item.ToString();
            }
            Debug.WriteLine("str",str);
            return str;
        }
    }
}
